﻿#region using
using System;
using AifMessageHeaderEditor.Lib.File;
using AifMessageHeaderEditor.Lib.Bsc; 
#endregion

namespace AifMessageHeaderEditor.Cmd
{
    class Program
    {
        static void Main(string[] args)
        {
            InitializeComandLineParser();

            try
            {
                ParseArguments(args);
            }
            catch (System.IO.FileNotFoundException ex)
            {
                Console.WriteLine("==================================================");
                Console.WriteLine("Exception caught:");
                Console.WriteLine(ex.Message);
                Console.WriteLine("Please choose an existing Aif message file.");
                PrintUsage();
            }
            catch (System.Security.Principal.IdentityNotMappedException ex)
            {
                Console.WriteLine("==================================================");
                Console.WriteLine("Exception caught:");
                Console.WriteLine(ex.Message);
                Console.WriteLine("Please check the username for the user and owner.");
                PrintUsage();
            }
            catch (Exception ex)
            {
                Console.WriteLine("==================================================");
                Console.WriteLine("Exception caught:");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.ToString());
                PrintUsage();
            }
        }

        private static void InitializeComandLineParser()
        {
            //
            // Define the required parameters.
            //
            string[] requiredArguments = {
            "-src",
            "-dest",
            "-owner",
            "-user",
        };
            CommandLineArgumentParser.DefineRequiredParameters(requiredArguments);

            //
            // Define the optional parameters along with their default values.
            //
            string[] optionalArguments = {
            "-nmid = y",
        };
            CommandLineArgumentParser.DefineOptionalParameter(optionalArguments);
        }

        private static void ParseArguments(string[] args)
        {
            //
            // Handle the special case "-help" separately
            //
            if (args.Length == 1 && args[0].Trim() == "-help")
            {
                PrintUsage();
            }
            else
            {
                CommandLineArgumentParser.ParseArguments(args);
                string src = CommandLineArgumentParser.GetParamValue("-src");
                string dest = CommandLineArgumentParser.GetParamValue("-dest");
                string owner = CommandLineArgumentParser.GetParamValue("-owner");
                string user = CommandLineArgumentParser.GetParamValue("-user");
                string nmid = CommandLineArgumentParser.GetParamValue("-nmid");

                if (!System.IO.File.Exists(src))
                    throw new System.IO.FileNotFoundException(src);

                MessageFile.Instance.LoadDocument(src);
                MessageFile.Instance.MessageFileInformation.HeaderNode.SourceEndpointUser = user;
                if (nmid == "y")
                    MessageFile.Instance.MessageFileInformation.HeaderNode.MessageId = string.Format("{{{0}}}", 
                        Guid.NewGuid().ToString());

                MessageFile.Instance.WriteDocument();
                Console.WriteLine("==================================================");
                Console.WriteLine("New file created: " + dest);
            }
        }

        private static void PrintUsage()
        {
            Console.WriteLine();
            Console.WriteLine("USAGE:");
            Console.WriteLine("AifMessageHeaderEditor.Cmd.exe -src <fileName> -dest <directory> -owner <userName> -user <userName>");
            Console.WriteLine("   [-nmid]");
            Console.WriteLine();
            Console.WriteLine("OPTIONS:");
            Console.WriteLine("  - src: the xml-source file.");
            Console.WriteLine("  - dest: the xml-destination directory.");
            Console.WriteLine("  - owner: the file-owner.");
            Console.WriteLine("  - user: the source endpoint user.");
            Console.WriteLine("  - nmid: y/n : create new message ID");
            Console.WriteLine();
            Console.WriteLine("EXAMPLE:");
            Console.WriteLine(" > AifMessageHeaderEditor.Cmd.exe -src c:\\file1.xml -dest c:\\ owner Administrator user Administrator");
            Console.WriteLine();
        }
    }
}